package com.wzdigit.wms.wms.service.inv.count;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.wzdigit.wms.basic.client.Pager;
import com.wzdigit.wms.basic.client.PagerInfo;
import com.wzdigit.wms.basic.domain.basic.Warehouse;
import com.wzdigit.wms.basic.enums.inventory.count.dtl.InvDtlStatusEnum;
import com.wzdigit.wms.basic.enums.inventory.count.dtl.InvTaskTypeEnum;
import com.wzdigit.wms.basic.enums.inventory.count.header.InvHeaderStatusEnum;
import com.wzdigit.wms.basic.mapper.core.basic.WarehouseMapper;
import com.wzdigit.wms.basic.service.code.WhsPermService;
import com.wzdigit.wms.wms.domain.dto.inv.count.InvCountHeaderRequest;
import com.wzdigit.wms.wms.domain.dto.inv.count.InvCountHeaderResponse;
import com.wzdigit.wms.wms.domain.dto.inv.count.InvCountSapDifResponse;
import com.wzdigit.wms.wms.domain.inv.count.InvCountDtl;
import com.wzdigit.wms.wms.domain.inv.count.InvCountHeader;
import com.wzdigit.wms.wms.mapper.inv.count.InvCountDtlMapper;
import com.wzdigit.wms.wms.mapper.inv.count.InvCountHeaderMapper;
import lombok.extern.slf4j.Slf4j;
import net.dreamlu.mica.core.utils.BeanUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@Service
@Slf4j
public class StockCountService {

    @Autowired
    InvCountHeaderMapper headerMapper ;

    @Autowired
    InvCountDtlMapper dtlMapper;

    @Autowired
    private WhsPermService whsCodeService;
    @Autowired
    InvCountDtlMapper invCountDtlMapper;

    @Autowired
    WarehouseMapper warehouseMapper;

    @Transactional
    public PagerInfo<List<InvCountHeaderResponse>> listPage(InvCountHeaderRequest headerRequest, Pager<?> pager,String loginName) {
        //区分初盘和复盘
        List<Integer>  statusList = null;
        if(headerRequest.getIsFirst()){
            statusList = Arrays.asList(InvHeaderStatusEnum.APPROVED.code,
                                       InvHeaderStatusEnum.WAIT_INV.code,
                                       InvHeaderStatusEnum.ING_INV.code,
                                       InvHeaderStatusEnum.FINISH_INV.code);
        }else{
            statusList = Arrays.asList(InvHeaderStatusEnum.WAIT_REL_INV.code,
                                        InvHeaderStatusEnum.ING_REL_INV.code,
                                        InvHeaderStatusEnum.FINISH_REL_INV.code);
        }
        // 当前用户权限的仓库
        List<Integer> whsIdList  = whsCodeService.getWareHouseIds(loginName);
        //是否只查看本人盘点任务
        if(!headerRequest.getIsSelf()){
            loginName="";
        }

        if(headerRequest.getInvCountDateLong()!=null){
            String format = DateUtil.format( new Date(headerRequest.getInvCountDateLong()), "yyyy-MM-dd");
            headerRequest.setInvCountDateString(format);
        }
        Page<?> page = PageHelper.offsetPage(pager.getSkipCount(), pager.getMaxResultCount());
        PagerInfo<List<InvCountHeaderResponse>> pagerInfo = new PagerInfo<>();
        List<InvCountHeaderResponse> list = headerMapper.selectAll(headerRequest,whsIdList,statusList,loginName);
        for (InvCountHeaderResponse item : list){
            //查询仓库名称
            Warehouse warehouse = warehouseMapper.selectById(item.getWhsId());
            item.setWhsName(warehouse.getWhsName());
            //查询盘点明细总数量和完成数量(查询初盘总数)
            Integer totalCount = invCountDtlMapper.selectTotalCountByHeaderId(item.getInvCountHeaderId(),null,headerRequest.getIsFirst());
            //查询已完成数量
            item.setTotalQty(totalCount);
            Integer compelateCount = invCountDtlMapper.selectTotalCountByHeaderId(item.getInvCountHeaderId(), InvDtlStatusEnum.FINISH.code,headerRequest.getIsFirst());
            item.setCompelateQty(compelateCount);
        }
        pagerInfo.setTotalCount(page.getTotal());
        pagerInfo.setItems(list);
        return pagerInfo;
    }


    public InvCountHeaderResponse getInvCountHeader(Integer headerId) {
        InvCountHeader invCountHeader = headerMapper.selectById(headerId);
        InvCountHeaderResponse response =new InvCountHeaderResponse();
        BeanUtil.copyNonNull(invCountHeader,response);
        //设置总数量
        QueryWrapper<InvCountDtl> wrapper = new QueryWrapper();
        wrapper.setEntity(new InvCountDtl().setInvCountHeaderId(new BigDecimal(headerId)));
        List<InvCountDtl> invCountDtls = dtlMapper.selectList(wrapper);
        int compelateQty = invCountDtls.stream().filter(inv -> inv.getStatus() == InvDtlStatusEnum.FINISH.code).collect(Collectors.toList()).size();
        response.setCompelateQty(compelateQty);
        response.setTotalQty(invCountDtls.size());
        return  response;
    }

}
